//
//  CommentSectionHeaderView.m
//  ZhiHuDaily
//
//  Created by albert on 16/8/18.
//  Copyright © 2016年 albert. All rights reserved.
//

#import "CommentSectionHeaderView.h"

@interface CommentSectionHeaderView ()
@property (weak, nonatomic) UILabel *cmtCountLabel;
@property (weak, nonatomic) UIButton *unfoldButton;
/** 分隔线 */
@property (nonatomic, weak) UIView *separatorView;
@end

@implementation CommentSectionHeaderView

/** 类方法 初始化 */
+ (instancetype)headerViewWithTableView:(UITableView *)tableView{
    
    static NSString *commentSectionHeaderID = @"sectionHeader";
    
    CommentSectionHeaderView *sectionHeaderView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:commentSectionHeaderID];
    if (sectionHeaderView == nil) {//缓存池中没有，自己创建
        sectionHeaderView = [[CommentSectionHeaderView alloc] initWithReuseIdentifier:commentSectionHeaderID];
    }
    return sectionHeaderView;
}

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        [self setup];
    }
    return self;
}

#pragma mark - 初始化
- (void)setup{
    UILabel *cmtCountLabel = [[UILabel alloc] init];
    cmtCountLabel.textColor = [UIColor blackColor];
    cmtCountLabel.font = [UIFont boldSystemFontOfSize:16];
    [self.contentView addSubview:cmtCountLabel];
    self.cmtCountLabel = cmtCountLabel;
    
    UIButton *unfoldButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [unfoldButton setImage:[UIImage imageNamed:@"Comment_Icon_Fold"] forState:(UIControlStateNormal)];
    [unfoldButton setImage:[UIImage imageNamed:@"Comment_Icon_Fold_Selected"] forState:(UIControlStateSelected)];
    unfoldButton.hidden = YES;
    [self.contentView addSubview:unfoldButton];
    self.unfoldButton = unfoldButton;
    
    UIView *separatorView = [[UIView alloc] init];
    separatorView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    [self.contentView addSubview:separatorView];
    self.separatorView = separatorView;
}

#pragma mark - 布局子控件
- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.cmtCountLabel.height = self.height;
    self.cmtCountLabel.width = 150;
    self.cmtCountLabel.x = 10;
    self.cmtCountLabel.y = 0;
    
    self.unfoldButton.width = self.height;
    self.unfoldButton.height = self.height;
    self.unfoldButton.x = self.width - 10 - self.unfoldButton.width;
    self.unfoldButton.y = 0;
    
    self.separatorView.frame = CGRectMake(0, self.height - 1, self.width, 1);
}

#pragma mark - 外部调用方法
/** unfoldButton添加事件 */
- (void)unfoldButtonAddTarget:(id)target action:(SEL)sel forControlEvents:(UIControlEvents)controlEvents{
    [self.unfoldButton addTarget:target action:sel forControlEvents:controlEvents];
}

/** 刷新表格后恢复unfoldButton的选中状态 */
- (void)resetUnfoldButtonSelected:(BOOL)shortCmtUnfolded{
    self.unfoldButton.selected = shortCmtUnfolded;
}

#pragma mark - 属性赋值
- (void)setCmtCountText:(NSString *)cmtCountText{
    _cmtCountText = [cmtCountText copy];
    
    self.cmtCountLabel.text = self.cmtCountText;
}

- (void)setUnfoldButtonHidden:(BOOL)unfoldButtonHidden{
    _unfoldButtonHidden = unfoldButtonHidden;
    
    self.unfoldButton.hidden = unfoldButtonHidden;
}

#pragma mark - 更新皮肤
- (void)changeToNightSkin{ // 夜间
    self.contentView.backgroundColor = JKColor(53, 53, 53);
    self.cmtCountLabel.textColor = JKColor(186, 186, 186);
}

- (void)changeToDaySkin{ // 白天
    self.contentView.backgroundColor = JKColor(223, 223, 223);
    self.cmtCountLabel.textColor = [UIColor blackColor];
}
@end
